import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/layout/Layout.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: ()=>import('../views/login/login.vue')
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    model:'hash',
    redirect:"/login",
    beforeEnter:(to,from,next)=> {
      if(to.path === '/home' ||
          to.path === '/donorinfo'||
          to.path === '/bloodbanks'||
          to.path === '/history'||
          to.path === '/distribute'||
          to.path === '/countIndex'||
          to.path === '/sites'||
          to.path === '/cars'||
          to.path === '/person') {
        let name = localStorage.getItem('name')
        let usertype=JSON.parse(localStorage.getItem('name')).userType
        console.log(usertype)
        if(usertype === '0' && name){
          next()
        } else {
          window.alert("用户没有访问权限！")
          // router.go(-1)
          this.$router.push('/login')
        }
      }
    },
    children:[
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/HomeView.vue'),
      },
      {
        path: '/donorinfo',
        name: 'about',
        component: () => import('../views/Donorinfo.vue')
      },
      {
        path: '/cascader',
        name: 'Cascader.vue',
        component: ()=> import('../views/Cascader.vue')
      },
      {
        path: '/person',
        name: 'Person.vue',
        component: () => import('../views/Person.vue')
      },
      {
        path: '/bloodbanks',
        name:'Bloodbanks.vue',
        component: () => import('../views/Bloodbanks.vue')
      },
      {
        path: '/history',
        name:'History.vue',
        component: () => import('../views/History.vue')
      },
      {
        path: '/distribute',
        name:'Distribute.vue',
        component: () => import('../views/Distribute.vue')
      },
      {
        path: '/countIndex',
        name:'CountIndex.vue',
        component: () => import('../views/CountIndex.vue')
      },
      {
        path: '/sites',
        name:'Donationsites.vue',
        component: () => import('../views/Donationsites.vue')
      },
      {
        path: '/cars',
        name:'Donationcars.vue',
        component: () => import('../views/Donationcars.vue')
      }
    ]
  },
  {
    path: '/index',
    name: 'index',
    component: ()=>import('../views/normaluserindex/index.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  // 1. 判断是不是登录页面
  // 是登录页面
  if(to.path === '/login') {
    next()
  } else {
    // 不是登录页面
    // 2. 判断 是否登录过
    let name = localStorage.getItem('name')
    name ? next() : next('/login')
  }
})

export default router
